package cn.me.alphamq.nameserver.event.model.cluster;

import cn.me.alphamq.common.event.model.Event;
import cn.me.alphamq.nameserver.enumeration.ReplicationMsgCodeEnum;
import cn.me.alphamq.nameserver.store.ServiceInstance;

/**
 * 复制消息事件，在服务实例的数据变化时产生，使用于节点之间的复制消息传递
 *
 * @author f
 */
public class ReplicationMsgEvent extends Event {

    /**
     * 复制消息的类型，添加/更新 还是 删除
     *
     * @see ReplicationMsgCodeEnum
     */
    private Integer msgCode;

    /**
     * 服务实例在 ServiceInstanceManager 的 map 中对应的键
     *
     * @see cn.me.alphamq.nameserver.store.ServiceInstanceManager
     */
    private String key;

    /**
     * 待复制的服务实例
     */
    private ServiceInstance serviceInstance;

    public ReplicationMsgEvent() {
    }

    /**
     * 构造一个 添加/更新 的复制消息事件
     *
     * @param msgId           消息的唯一 id
     * @param serviceInstance 服务实例
     */
    public ReplicationMsgEvent(String msgId, ServiceInstance serviceInstance) {
        super(msgId);
        this.msgCode = ReplicationMsgCodeEnum.INSERT_OR_UPDATE.getCode();
        this.serviceInstance = serviceInstance;
    }

    /**
     * 构造一个 删除 的复制消息事件
     *
     * @param key 服务实例对应的 key
     */
    public ReplicationMsgEvent(String key) {
        this.msgCode = ReplicationMsgCodeEnum.DELETE.getCode();
        this.key = key;
    }

    public Integer getMsgCode() {
        return msgCode;
    }

    public void setMsgCode(Integer msgCode) {
        this.msgCode = msgCode;
    }

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public ServiceInstance getServiceInstance() {
        return serviceInstance;
    }

    public void setServiceInstance(ServiceInstance serviceInstance) {
        this.serviceInstance = serviceInstance;
    }
}
